package com.service.admin;

import com.bean.bo.GoodTypeAdd;
import com.bean.bo.good.*;
import com.bean.bo.message.ReplyBO;
import com.bean.pojo.Good;
import com.bean.pojo.GoodsType;
import com.bean.pojo.Space;
import com.bean.vo.goods.GoodDetailsVO;
import com.bean.vo.goods.GoodVO;
import com.bean.vo.goods.SpecVO;
import com.bean.vo.goods.TypeGoodVO;
import com.bean.vo.message.ShowNoReplyMessageVO;
import com.bean.vo.message.ShowReplyMessageVO;
import com.dao.admin.GoodsMapper;
import com.sun.jmx.snmp.internal.SnmpSubSystem;
import com.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Good service
 */
public class GoodsServiceImplement implements GoodService{
    @Override
    public List<GoodsType> showAllGoodsTypes() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsType> goodsTypesList = mapper.selectAllGoodsTypes();
        sqlSession.close();
        return goodsTypesList;
    }

    /**
     * 添加商品分类
     * @param  goodTypeAdd 商品分类的实例对象
     * @return 200/404
     */
    @Override
    public int addGoodsType(GoodTypeAdd goodTypeAdd) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        GoodsType goodsType = new GoodsType(null, goodTypeAdd.getName());
        int insertState = 0;
        try {
            insertState = mapper.insertType(goodsType);
        } catch (Exception e) {
            return 404;
        }
        sqlSession.close();
        if (insertState==1){
            return 200;
        }
        return 404;

    }

    /**
     *查询
     * @param typeId 种类id
     * @return typeGoodVOSList
     */
    @Override
    public List<TypeGoodVO> selectToJson(Integer typeId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<TypeGoodVO> typeGoodVOSList = mapper.selectLeftJoinSpace(typeId);
        sqlSession.close();
        return typeGoodVOSList;
    }


    /**
     * 添加商品
     * @param addGoodBO Json数据封装的对象实例
     * @return 200/404
     */
    @Override
    public int addGood(AddGoodBO addGoodBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<SpaceBO> spaceList = addGoodBO.getSpecList();
        ArrayList<Space> spaces = new ArrayList<>();
        //找出最小的价格
        double price=Double.parseDouble(spaceList.get(0).getUnitPrice());
        for (SpaceBO spaceBO : spaceList) {
            if (Double.parseDouble(spaceBO.getUnitPrice())<price){
                price=Double.parseDouble(spaceBO.getUnitPrice());
            }
        }
        Good good = new Good(null, addGoodBO.getName(), addGoodBO.getImg(), price, Integer.parseInt(addGoodBO.getTypeId()), addGoodBO.getDesc());
          int insertState = mapper.insertGood(good);
        //将对应的商品id存入space
        for (SpaceBO spaceBO : spaceList) {
            Space space = new Space(null, spaceBO.getSpecName(), Integer.parseInt(spaceBO.getStockNum()), Double.parseDouble(spaceBO.getUnitPrice()),good.getId());
            spaces.add(space);
        }
        int insertSpaceState = mapper.insertSpace(spaces);
        sqlSession.close();
        if (insertState==1&&insertSpaceState==spaces.size()){
            return 200;
        }
        return 404;
    }

    /**
     * 删除商品
     * @param id 商品id
     * @return 200/404
     */
    @Override
    public int deleteGood(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        int deleteGoodState = mapper.deleteGoodById(id);
        int deleteSpaceState = mapper.deleteSpaceById(id);
        sqlSession.close();
        if (deleteGoodState==1){
            return 200;
        }
        return 404;
    }

    /**
     *
     * @param id
     * @return GoodDetailsVO
     */
    @Override
    public GoodDetailsVO showGoodDetails(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        //要封装的VO对象
        GoodDetailsVO goodDetailsVO = new GoodDetailsVO();
        ArrayList<SpecVO> specVOList = new ArrayList<>();
        //查询到的原始数据
        Good good = mapper.selectGoodById(id);
        List<Space> spacesList = mapper.selectSpaceByGoodId(id);
        sqlSession.close();
        //VO对象的封装
        GoodVO goodVO = new GoodVO(good.getDetails(),good.getId(),good.getImg(),good.getName(),good.getTypeId(),good.getPrice());
        goodDetailsVO.setGoodVO(goodVO);
        //转换spaceList到specVOList
        for (Space space : spacesList) {
            SpecVO specVO = new SpecVO(space.getId(), space.getSpecName(), space.getStockNum(), space.getUnitPrice());
            specVOList.add(specVO);
        }
        goodDetailsVO.setSpecs(specVOList);


        return goodDetailsVO;

    }

    /**
     * 添加规格
     * @param addSpaceBO
     * @return
     */
    @Override
    public int addGoodSpaceByUpdate(AddSpaceBO addSpaceBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        Space space = new Space(null, addSpaceBO.getSpecName(), addSpaceBO.getStockNum(), addSpaceBO.getUnitPrice(), addSpaceBO.getGoodsId());
        int insertState = mapper.insertSpaceBySpaceObj(space);
        sqlSession.close();
        if (insertState==1){
            return 200;

        }
        return 404;

    }

    /**
     * 删除规格
     * @param spaceBOByUpdate
     * @return
     */
    @Override
    public int deleteSpaceByUpdate(SpaceBOByUpdate spaceBOByUpdate) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        int deleteState = mapper.deleteSpaceByGoodIdAndSpecName(spaceBOByUpdate.getGoodsId(), spaceBOByUpdate.getSpecName());
        sqlSession.close();
        if (deleteState==1){
            return 200;
        }
        return 404;
    }

    /**
     * 修改商品
     * @param updateGoodBO
     * @return
     */
    @Override
    public int updateGood(UpdateGoodBO updateGoodBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<SpecVO> specList = updateGoodBO.getSpecList();
        double price = specList.get(0).getUnitPrice();
        double newPrice=0;
        //求最小价格
        for (SpecVO specVO : specList) {
            if (price<specList.get(0).getUnitPrice()){
                price=specList.get(0).getUnitPrice();
            }
        }
        double oldPrice = mapper.selectPriceByGoodId(updateGoodBO.getId());
        if (price<oldPrice){
            newPrice=price;
        }
        else {
            newPrice=oldPrice;
        }
        //封装good对象
        Good good = new Good(updateGoodBO.getId(), updateGoodBO.getName(), updateGoodBO.getImg(),
                newPrice, updateGoodBO.getTypeId(), updateGoodBO.getDesc());
        //执行语句后的返回值
        int updateState = mapper.updateGoodByGoodObj(good);
        //封装space对象
        ArrayList<Space> spacesList = new ArrayList<>();
        for (SpecVO specVO : specList) {
            Space space = new Space(specVO.getId(), specVO.getSpecName(), specVO.getStockNum(), specVO.getUnitPrice());
            spacesList.add(space);
        }
        //逐条实现修改
        int updateSpaceState=0;
        for (Space space : spacesList) {
            int i = mapper.updateSpaceBySpace(space);
            updateSpaceState+=i;
        }
        sqlSession.close();
        if (updateState==1&&updateSpaceState!=0){
            return 200;
        }

        return 404;
    }

    /**
     * 展示未回复留言
     * @return
     */
    @Override
    public List<ShowNoReplyMessageVO> showMessage() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<ShowNoReplyMessageVO> showNoReplyMessageVO = mapper.selectShowNoReplyMessageVO();
        return showNoReplyMessageVO;
    }

    /**
     * 展示已回复留言
     * @return
     */
    @Override
    public List<ShowReplyMessageVO> showReplyMessage() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<ShowReplyMessageVO> showReplyMessageVOS = mapper.selectShowReplyMessageVO();
        return showReplyMessageVOS;
    }

    /**
     * 回复留言
     * @param replyBO
     * @return
     */
    @Override
    public int replyMessage(ReplyBO replyBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        int updateState = mapper.updateReply(replyBO);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        String time = sf.format(date);
        replyBO.setUpdateTime(time);
        int i = mapper.updateTime(replyBO);
        if (updateState!=0&&i!=0){
            return 200;
        }

        return 404;
    }

}
